package com.demo;

//import lombok.extern.slf4j.Slf4j;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

//@Slf4j
public class Main {

    private static int port = 8087;
    public static void main(String[] args) {
//        args = new String[]{"8088"};
        Main manager = new Main();
        if(args.length!=1){
            System.out.println("需要输入端口号");
        }
        String portP = args[0];
        Main.port = Integer.parseInt(portP);
        System.out.println("监听端口号："+port);
        manager.doListen();
    }

    public void doListen() {
        ServerSocket server;
        try {
            server = new ServerSocket(Main.port );
            while (true) {
                Socket client = server.accept();
                new Thread(new SSocket(client)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class SSocket implements Runnable {
        Socket client;

        public SSocket(Socket client) {
            this.client = client;
        }

        public void run() {
            DataInputStream input;
            DataOutputStream output;
            try {
                input = new DataInputStream(client.getInputStream());
                output = new DataOutputStream(client.getOutputStream());
                Thread.sleep(5000L);
                String data = input.readUTF();
                output.writeUTF("Recive1:  " + data);
//                log.info("接口名称 access {}", data);
                System.out.println("接口名称 access {}"+ data);
                data = input.readUTF();
                output.writeUTF("Recive2:  " + data);
//                log.info("接收的数据 {}", data);
                System.out.println("接收的数据 {}"+ data);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {}
        }
    }
}
